package com.guoyansoft.config;

import com.guoyansoft.model.pojo.Administrators;
import com.guoyansoft.model.service.AdministratorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 刘耀诚
 * 该类用于对前端请求进行拦截
 */

//@Component
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private AdministratorsService administratorsService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //        前端发来的OPTIONS请求一定要放行
        if("OPTIONS".equalsIgnoreCase(request.getMethod())){
            return true;
        }
        String authValue = request.getHeader("Authorization");
        if (authValue==null||!authValue.contains(",")){
            return false;
        }
        String[] authArray = authValue.split(",");
        String userName=authArray[0];
        String userPass=authArray[1];
        R r = administratorsService.queryOfLogin(new Administrators(null, userName, userPass, null, null));
        if(r.getData()!=null){
            return true;
        }
        return false;
    }
}
